package com.minelx.scriptbuilder.parse.parser;

import com.minelx.scriptbuilder.script.IVariable;
import com.minelx.scriptbuilder.script.operation.ReturnOperation;
import com.minelx.sequence.IStringScanner;
import com.minelx.sequence.ReturnMatcher;
import com.minelx.scriptbuilder.script.ScriptContext;
import com.minelx.scriptbuilder.script.operation.IOperation;

public class ReturnParser implements ReturnMatcher<IOperation, IStringScanner> {

	private final ScriptContext context;

	private final ValuesParser parser;

	public ReturnParser(ScriptContext context) {
		this.context = context;
		parser = new ValuesParser(context);
	}

	@Override
	public boolean isMatch(IStringScanner scanner) {
		return scanner.exists("return");
	}

	@Override
	public IOperation onMatched(IStringScanner scanner) {
		scanner.pass("return");
		scanner.trim();
		IVariable returnVariable = parser.parse(scanner);
		scanner.nextChar();
		return new ReturnOperation(context, returnVariable);
	}
}
